package com.ruoyi.display.controller;

import com.ruoyi.base.domain.School;
import com.ruoyi.base.service.IDeviceService;
import com.ruoyi.base.service.ISchoolService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.data.service.ITdataHtEqService;
import com.ruoyi.data.service.ITdataHtWgService;
import com.ruoyi.data.service.ITdataRtRsService;
import com.ruoyi.display.domain.Electricity;
import com.ruoyi.display.domain.HourViolations;
import com.ruoyi.display.domain.RealtimeData;
import com.ruoyi.display.domain.Violations;
import com.ruoyi.display.domain.ViolationsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/display")
@Api(tags = "大屏接口")
public class DisplayController extends BaseController {

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private ISchoolService schoolService;

    @Autowired
    private ITdataHtWgService tDataHtWgService;

    @Autowired
    private ITdataHtEqService tdataHtEqService;

    @Autowired
    private ITdataRtRsService tdataRtRsService;

    @GetMapping("/deviceState")
    @ApiOperation("查询工作状态列表")
    public AjaxResult deviceState() {
        return AjaxResult.success(deviceService.selectDeviceState());
    }

    @GetMapping("/violations")
    @ApiOperation("违规监控")
    public AjaxResult violations() {
        List<Violations> violations = tDataHtWgService.todayViolations();
        return AjaxResult.success(new ViolationsVo()
                .setToday(violations.size())
                .setMonth(tDataHtWgService.monthViolations())
                .setTodayViolations(violations));
    }

    @GetMapping("/hourViolations")
    @ApiOperation("违规统计")
    public AjaxResult hourViolations(@RequestParam("date") String date) {

        List<HourViolations> queryHourViolations = tDataHtWgService.hourViolations(date);
        List<HourViolations> hourViolationsList = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            HourViolations hourViolationsVo = new HourViolations().setCount(0);
            String hour = String.format("%02d", i);
            for (HourViolations hourViolations : queryHourViolations) {
                if (Objects.equals(hourViolations.getHour(), hour)) {
                    hourViolationsVo.setCount(hourViolations.getCount());
                    break;
                }
            }
            hourViolationsList.add(hourViolationsVo.setHour(hour + "时"));
        }
        return AjaxResult.success(hourViolationsList);
    }

    @GetMapping("/electricity")
    @ApiOperation("用电分析")
    public AjaxResult electricity(@RequestParam("date") String date) {

        List<Electricity> queryElectricity = tdataHtEqService.electricity(date);
        List<Electricity> electricityList = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            Electricity electricityVo = new Electricity().setCount(0);
            String hour = String.format("%02d", i);
            for (Electricity electricity : queryElectricity) {
                if (Objects.equals(electricity.getHour(), hour)) {
                    electricityVo.setCount(electricity.getCount());
                    break;
                }
            }
            electricityList.add(electricityVo.setHour(hour + "时"));
        }
        return AjaxResult.success(electricityList);
    }

    @GetMapping("/realtime")
    @ApiOperation("中间的实时数据")
    public AjaxResult realtime() {
        BigDecimal todayRs = BigDecimal.ZERO;
        BigDecimal todaySubtract = tdataRtRsService.selectTodayTotalRs().subtract(tdataRtRsService.selectYesterdayTotalRs());
        if (todaySubtract.compareTo(BigDecimal.ZERO) > 0) {
            todayRs = todaySubtract;
        }
        List<School> schools = schoolService.selectSchoolList(null);

        RealtimeData realtimeData = new RealtimeData()
                .setAreaEnergy(todayRs.intValue() == 0 ? BigDecimal.ZERO : todayRs.divide(schools.get(0).getArea(), 2, RoundingMode.HALF_EVEN))
                .setPersonEnergy(todayRs.intValue() == 0 ? BigDecimal.ZERO : todayRs.divide(BigDecimal.valueOf(schools.get(0).getPerson()), 2, RoundingMode.HALF_EVEN))
                .setLiveElectricity(todayRs)
                .setTodayElectricity(todayRs)
                .setYesterdayElectricity(tdataRtRsService.selectYesterdayRs())
                .setLastMonthElectricity(tdataRtRsService.selectLastMonthEq())
                .setMonthElectricity(tdataRtRsService.selectMonthEq());

        realtimeData.setTodayElectricityCompare(realtimeData.getYesterdayElectricity().intValue() == 0 ? BigDecimal.ZERO :
                        realtimeData.getTodayElectricity().subtract(realtimeData.getYesterdayElectricity()).multiply(BigDecimal.valueOf(100))
                                .divide(realtimeData.getYesterdayElectricity(), 2, RoundingMode.HALF_EVEN))
                .setMonthElectricityCompare(realtimeData.getLastMonthElectricity().intValue() == 0 ? BigDecimal.ZERO :
                        realtimeData.getMonthElectricity().subtract(realtimeData.getLastMonthElectricity()).multiply(BigDecimal.valueOf(100))
                                .divide(realtimeData.getLastMonthElectricity(), 2, RoundingMode.HALF_EVEN))
        ;
        return AjaxResult.success(realtimeData);
    }
}
